package com.ztorn.fiscale.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ztorn.fiscale.mapper.FiscaleRefundMatchingMapper;
import com.ztorn.fiscale.mapper.FiscaleCustomsDeclarationMapper;
import com.ztorn.fiscale.mapper.FiscaleTaxInvoiceMapper;
import com.ztorn.fiscale.domain.FiscaleRefundMatching;
import com.ztorn.fiscale.domain.FiscaleCustomsDeclaration;
import com.ztorn.fiscale.domain.FiscaleTaxInvoice;
import com.ztorn.fiscale.service.IFiscaleRefundMatchingService;

/**
 * 配单记录Service业务层处理
 *
 * @author ztorn
 * @date 2025-09-20
 */
@Service
public class FiscaleRefundMatchingServiceImpl implements IFiscaleRefundMatchingService
{
    @Autowired
    private FiscaleRefundMatchingMapper fiscaleRefundMatchingMapper;

    @Autowired
    private FiscaleCustomsDeclarationMapper fiscaleCustomsDeclarationMapper;

    @Autowired
    private FiscaleTaxInvoiceMapper fiscaleTaxInvoiceMapper;

    /**
     * 查询配单记录
     *
     * @param matchingId 配单记录ID
     * @return 配单记录
     */
    @Override
    public FiscaleRefundMatching selectFiscaleRefundMatchingById(Long matchingId)
    {
        FiscaleRefundMatching matching = fiscaleRefundMatchingMapper.selectFiscaleRefundMatchingById(matchingId);
        if (matching != null) {
            // 查询关联的报关单信息
            FiscaleCustomsDeclaration declaration = fiscaleCustomsDeclarationMapper.selectFiscaleCustomsDeclarationByDeclarationId(matching.getDeclarationId());
            matching.setDeclaration(declaration);

            // 查询关联的发票信息
            FiscaleTaxInvoice invoice = fiscaleTaxInvoiceMapper.selectFiscaleTaxInvoiceById(matching.getInvoiceId());
            matching.setInvoice(invoice);
        }
        return matching;
    }

    /**
     * 查询配单记录列表
     *
     * @param fiscaleRefundMatching 配单记录
     * @return 配单记录
     */
    @Override
    public List<FiscaleRefundMatching> selectFiscaleRefundMatchingList(FiscaleRefundMatching fiscaleRefundMatching)
    {
        List<FiscaleRefundMatching> list = fiscaleRefundMatchingMapper.selectFiscaleRefundMatchingList(fiscaleRefundMatching);
        for (FiscaleRefundMatching matching : list) {
            // 查询关联的报关单信息
            FiscaleCustomsDeclaration declaration = fiscaleCustomsDeclarationMapper.selectFiscaleCustomsDeclarationByDeclarationId(matching.getDeclarationId());
            matching.setDeclaration(declaration);

            // 查询关联的发票信息
            FiscaleTaxInvoice invoice = fiscaleTaxInvoiceMapper.selectFiscaleTaxInvoiceById(matching.getInvoiceId());
            matching.setInvoice(invoice);
        }
        return list;
    }

    /**
     * 新增配单记录
     *
     * @param fiscaleRefundMatching 配单记录
     * @return 结果
     */
    @Override
    public int insertFiscaleRefundMatching(FiscaleRefundMatching fiscaleRefundMatching)
    {
        return fiscaleRefundMatchingMapper.insertFiscaleRefundMatching(fiscaleRefundMatching);
    }

    /**
     * 修改配单记录
     *
     * @param fiscaleRefundMatching 配单记录
     * @return 结果
     */
    @Override
    public int updateFiscaleRefundMatching(FiscaleRefundMatching fiscaleRefundMatching)
    {
        return fiscaleRefundMatchingMapper.updateFiscaleRefundMatching(fiscaleRefundMatching);
    }

    /**
     * 批量删除配单记录
     *
     * @param matchingIds 需要删除的配单记录ID
     * @return 结果
     */
    @Override
    public int deleteFiscaleRefundMatchingByIds(Long[] matchingIds)
    {
        return fiscaleRefundMatchingMapper.deleteFiscaleRefundMatchingByIds(matchingIds);
    }

    /**
     * 删除配单记录信息
     *
     * @param matchingId 配单记录ID
     * @return 结果
     */
    @Override
    public int deleteFiscaleRefundMatchingById(Long matchingId)
    {
        return fiscaleRefundMatchingMapper.deleteFiscaleRefundMatchingById(matchingId);
    }

    /**
     * 取消配对
     *
     * @param matchingId 配单ID
     * @return 结果
     */
    @Override
    public int cancelMatching(Long matchingId)
    {
        FiscaleRefundMatching matching = new FiscaleRefundMatching();
        matching.setMatchingId(matchingId);
        matching.setStatus("cancelled");
        return fiscaleRefundMatchingMapper.updateFiscaleRefundMatching(matching);
    }

    /**
     * 手动关联报关单和发票
     *
     * @param declarationId 报关单ID
     * @param invoiceId 发票ID
     * @param matchReason 匹配原因
     * @return 结果
     */
    @Override
    public int manualMatching(Long declarationId, Long invoiceId, String matchReason)
    {
        // 检查是否已存在配单记录
        FiscaleRefundMatching existing = fiscaleRefundMatchingMapper.selectFiscaleRefundMatchingByDeclarationIdAndInvoiceId(declarationId, invoiceId);
        if (existing != null) {
            return 0; // 已存在配单记录
        }

        // 创建新的配单记录
        FiscaleRefundMatching matching = new FiscaleRefundMatching();
        matching.setDeclarationId(declarationId);
        matching.setInvoiceId(invoiceId);
        matching.setMatchingType("manual");
        matching.setMatchReason(matchReason);
        matching.setStatus("matched");
        return fiscaleRefundMatchingMapper.insertFiscaleRefundMatching(matching);
    }
}
